package com.syyc.utils;

import com.syyc.pojo.Content;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huimin
 * @date 2021/10/21
 * @apiNote
 */
public class HtmlParseUtil {
    public static void main(String[] args) throws IOException {
        //new HtmlParseUtil().parseBlibli("java");
        //new HtmlParseUtil().parseBilibli();
    }

    public List<Content> parseBlibli(String keyword) throws IOException {

        // 获得请求
        String url = "https://search.bilibili.com/all?keyword=" + keyword;

        // 解析网页
        Document document = Jsoup.parse(new URL(url), 300000);

        // 所有你在js中可以使用的方法，这里都能用。
        Element element = document.getElementsByAttributeValue("type", "video").get(0);

        Elements elements = element.getElementsByTag("li");

        List<Content> list = new ArrayList<>();

        for (Element el : elements) {
            String img = el.getElementsByClass("lazy-img").get(0).getElementsByTag("img").get(0).attr("src");
            //String img = el.getElementsByTag("img").eq(0).attr("data-lazy-img");
            System.out.println(img);

            String info = el.getElementsByClass("info").eq(0).get(0).getElementsByTag("a").eq(0).text();
            System.out.println(info);
            System.out.println("=========================");
        }

        return null;
    }

    public void parseBilibli() throws IOException {
        String keyword = "java";

        // 获得请求
        String url = "https://search.bilibili.com/all?keyword=" + keyword;

        Document document = Jsoup.parse(new URL(url), 30000);

        Element element = document.getElementById("all-list");

        Element elemen = element.getElementsByAttributeValue("type", "video").get(0);

        Elements ele = elemen.getElementsByTag("li");

        System.out.println(ele);

        for (Element el : ele) {
            String img = el.getElementsByTag("img").eq(0).attr("src");
            String href = el.getElementsByTag("a").eq(0).attr("href");
            String title = el.getElementsByTag("a").eq(1).text();

            System.out.println("title:" + title + "; img:" + img + "; href:" + href);
        }

    }

    public void parseFanju(){
        String url = "https://api.bilibili.com/pgc/season/index/result?season_version=-1&spoken_language_type=-1" +
                "&area=-1&is_finish=-1&copyright=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&order=3&" +
                "st=1&sort=0&page=1&season_type=1&pagesize=20&type=1";
    }


}
